package com.zadtek.sharkRider.frame;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.zadtek.sharkRider.activity.common.Const;

/**
 * 
 * @author 用于Android的http协议网络访问
 * 
 */
public class WebHttpHelper {

	/**
	 * 根据给定的url地址访问网络，得到响应内容(这里为GET方式访问)
	 * 
	 * @param url
	 *            指定的url地址
	 */
	public static String getWebContentGet(String url) {
		// 创建一个http请求对象
		HttpGet request = new HttpGet(url);
		// 创建HttpParams以用来设置HTTP参数
		HttpParams params = new BasicHttpParams();
		// 设置连接超时或响应超时
		HttpConnectionParams.setConnectionTimeout(params, 4000);
		HttpConnectionParams.setSoTimeout(params, 4000);
		// 创建一个网络访问处理对象
		HttpClient httpClient = new DefaultHttpClient(params);
		try {
			// 执行请求参数项
			HttpResponse response = httpClient.execute(request);
			// 判断是否请求成功
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 获得响应信息
				String content = EntityUtils.toString(response.getEntity());
				return content;
			} else {
				// 网连接失败，使用Toast显示提示信息
			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			// 释放网络连接资源
			httpClient.getConnectionManager().shutdown();
		}
		return null;
	}

	/**
	 * 根据给定的url地址访问网络，得到响应内容(这里为Post方式访问)
	 * 
	 * @param url
	 *            指定的url地址
	 */
	public static String getWebContentPost(String url,
			List<NameValuePair> params) {
		HttpClient client = new DefaultHttpClient();
		HttpEntity entity = null;
		try {
			HttpPost request = new HttpPost(url);
			HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
			// HttpEntity httpEntity = new UrlEncodedFormEntity(params);
			request.setEntity(httpEntity);
			HttpResponse response = client.execute(request);
			// 判断请求是否成功
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				entity = response.getEntity();
				String result = EntityUtils.toString(entity);
				return result;
			} else {
				// 网连接失败
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			// 关闭连接，释放资源
			try {
				if (entity != null)
					entity.consumeContent();
				client.getConnectionManager().shutdown();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 登录
	 * 
	 * @param ince
	 * @param uname
	 * @param pass
	 * @return
	 */
	public static String login_qs(String ince, String uname, String pass) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("uname", uname));
		params.add(new BasicNameValuePair("pass", pass));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 新任务——待抢单列表
	 * 
	 * @param ince
	 * @param lat
	 *            ——纬度值
	 * @param lng
	 *            ——经度值
	 * @return
	 */
	public static String getOrderList(String ince, String lat, String lng) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("lat", lat));
		params.add(new BasicNameValuePair("lng", lng));
		return getWebContentPost(Const.TOURL, params);
	}
	public static String getOrderList(String ince, String lat, String lng,String uid,String rr_last_id,String help_last_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("lat", lat));
		params.add(new BasicNameValuePair("lng", lng));
		params.add(new BasicNameValuePair("emp_id", uid));
		params.add(new BasicNameValuePair("rr_last_id", rr_last_id));
		params.add(new BasicNameValuePair("help_last_id", help_last_id));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 待取货列表
	 * 
	 * @param ince
	 * @param emp_id
	 *            ——快递ID
	 * @return
	 */
	public static String getGetorderList(String ince, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 配送中列表
	 * 
	 * @param ince
	 * @param emp_id
	 *            ————快递ID
	 * @return
	 */
	public static String getSendorderList(String ince, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 订单详情
	 * 
	 * @param ince
	 * @param main_id
	 * @param emp_id
	 * @return
	 */
	public static String orderinfo(String ince, String main_id, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("main_id", main_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 点击抢单
	 * 
	 * @param ince
	 * @param main_id
	 * @param emp_id
	 * @return
	 */
	public static String getQsQiang(String ince, String main_id, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("main_id", main_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}
	/**
	 * 随意购——点击抢单
	 * 
	 * @param ince
	 * @param main_id
	 * @param emp_id
	 * @return
	 */
	public static String getHelpQsQiang(String ince, String order_id, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("order_id", order_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.SYGTOURL, params);
	}

	/**
	 * 快递取货,确认取货
	 * 
	 * @param ince
	 * @param order_id
	 * @param emp_id
	 * @return
	 */
	public static String getfood(String ince, String order_id, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("order_id", order_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 已完成订单列表
	 * 
	 * @param ince
	 * @param emp_id
	 * @param page
	 * @return
	 */
	public static String getoverOrd(String ince, String emp_id, String page) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		params.add(new BasicNameValuePair("page", page));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 人人便利确认送达
	 * 
	 * @param ince
	 * @param order_id
	 * @param emp_id
	 * @return
	 */
	public static String getQsSendFood(String ince, String order_id,
			String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("order_id", order_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}
	/**
	 * 随意购确认送达
	 * 
	 * @param ince
	 * @param order_id
	 * @param emp_id
	 * @return
	 */
	public static String getQsEXSendFood(String ince, String order_id,
			String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("order_id", order_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.SYGTOURL, params);
	}

	/**
	 * 获取骑士信息
	 * 
	 * @param ince
	 * @param order_id
	 * @param emp_id
	 * @return
	 */
	public static String getQsinfo(String ince, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 设置快递员开工收工
	 * 
	 * @param ince
	 * @param emp_id
	 * @param close
	 * @return
	 */
	public static String setqsOpen(String ince, String emp_id, String close) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		params.add(new BasicNameValuePair("close", close));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 更新骑士坐标
	 * 
	 * @param ince
	 * @param emp_id
	 * @param lat
	 * @param lng
	 * @return
	 */
	public static String setEmpLocation(String ince, String emp_id, String lat,
			String lng) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		params.add(new BasicNameValuePair("lat", lat));
		params.add(new BasicNameValuePair("lng", lng));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 获取订单数量
	 * 
	 * @param ince
	 * @param emp_id
	 * @return
	 */
	public static String getOrderCount(String ince, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 配送员提现申请
	 * 
	 * @param ince
	 * @param emp_id
	 * @param amount
	 * @return
	 */
	public static String getDeposit(String ince, String emp_id, String amount) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		params.add(new BasicNameValuePair("amount", amount));
		return getWebContentPost(Const.TOURL, params);
	}

	/**
	 * 获取配送员账户明细------配送员提现申请
	 * 
	 * @param ince
	 * @param emp_id
	 * @param page
	 * @return
	 */
	public static String getEmpAccountlog(String ince, String emp_id,
			String page) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		params.add(new BasicNameValuePair("page", page));
		return getWebContentPost(Const.TOURL, params);
	}
	
	
	/**
	 * 随意购订单详情
	 * @param ince
	 * @param order_id
	 * @param emp_id
	 * @return
	 */
	public static String sygorderinfo(String ince, String order_id,
			String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("order_id", order_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.SYGTOURL, params);
	}
	
	/**
	 * 万能跑腿取货,确认取货
	 * 
	 * @param ince
	 * @param order_id
	 * @param emp_id
	 * @return
	 */
	public static String exgetfood(String ince, String order_id, String emp_id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("order_id", order_id));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		return getWebContentPost(Const.SYGTOURL, params);
	}
	
	/**
	 * 已完成万能跑腿订单列表
	 * 
	 * @param ince
	 * @param emp_id
	 * @param page
	 * @return
	 */
	public static String getsygoverOrd(String ince, String emp_id, String page) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", emp_id));
		params.add(new BasicNameValuePair("page", page));
		return getWebContentPost(Const.SYGTOURL, params);
	}
	
	
	/**
	 * 获取新版本的信息
	 * @param ince
	 * @return
	 */
	public static String getNewVersion(String ince) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		return getWebContentPost(Const.TOURL, params);
	}
	
	/**
	 * 获取骑士排行榜统计数据
	 * @param ince
	 * @return
	 */
	public static String getqstotalph(String ince, String time, String type) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("time", time));
		params.add(new BasicNameValuePair("type", type));
		return getWebContentPost(Const.TOURL, params);
	}
	
	/**
	 * 获取单个骑士统计数据
	 * @param ince
	 * @return
	 */
	public static String getoneqstotal(String ince, String empid, String time) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ince", ince));
		params.add(new BasicNameValuePair("emp_id", empid));
		params.add(new BasicNameValuePair("time", time));
		return getWebContentPost(Const.TOURL, params);
	}
	
}